package com.ruifu.pls.utils;

import com.ruifu.pls.model.test.GoodDetailsRequest;
import com.ruifu.pls.model.test.ParseJson;
import com.ruifu.pls.model.test.CarnumParams;
import com.ruifu.pls.model.GLCallInputParam;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtils {


    public static String doPost(String host, String url, String param) throws IOException {

        String encoding = "UTF-8";
        byte[] data = param.getBytes(encoding);
        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(host+url).openConnection();//请求路径
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/text");
            conn.setConnectTimeout(5 * 1000);
            BufferedOutputStream outStream = new BufferedOutputStream(conn.getOutputStream());
            outStream.write(data);
            outStream.flush();
            outStream.close();
            if (conn.getResponseCode() == 200) {//请求成功
                String readLine = new String();
                StringBuffer sb = new StringBuffer("");
                BufferedReader responseReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((readLine = responseReader.readLine()) != null) {
                    readLine = new String(readLine.getBytes(), "utf-8");
                    sb.append(readLine).append("\n");
                }
                responseReader.close();
                return sb.toString();
            }else
            {return "{\"StateCode\":1,\"Message\":\"request error\"" + conn.getResponseCode()+ "}"; }
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"StateCode\":1,\"Message\":\"request erroe\"" + e.getStackTrace() + "}";

        }

    }

    public static void main(String args[])
    {

        try{
            //创建请求参数
            CarnumParams cp = new CarnumParams();
            cp.setCarnum("\\u4eacA09999");//获取对应参数所给对方的以便于找到对应的位置。
            System.out.println(ParseJson.parse(cp));

            //
            GLCallInputParam glRquestParam = new GLCallInputParam( );
            glRquestParam.setData(ParseJson.parse(cp));
            glRquestParam.setMethod("g2matrix.commodities.creategoods");
            glRquestParam.setMd5Sigh(SighUtil.getMD5(SighUtil.signParam(glRquestParam).replace("\\\\","\\")));//



            String temp =SighUtil.signParam(glRquestParam);
//            System.out.println(temp.replace("\\\\","\\").equals("ab179020b82d2fdcd4cea176796f7156app_keyapitestdata{\"carnum\":\"\\u4eacA09999\"}methodips2.api.currentStatustimestamp2012-09-19 19:12:05ab179020b82d2fdcd4cea176796f7156"));
//            System.out.println("111:"+ "ab179020b82d2fdcd4cea176796f7156app_keyapitestdata{\"carnum\":\"\\u4eacA09999\"}methodips2.api.currentStatustimestamp2012-09-19 19:12:05ab179020b82d2fdcd4cea176796f7156");
//            System.out.println("111:"+ temp);
//            System.out.println("999:"+SighUtil.getMD5("ab179020b82d2fdcd4cea176796f7156app_keyapitestdata{\"carnum\":\"\\u4eacA09999\"}methodips2.api.currentStatustimestamp2012-09-19 19:12:05ab179020b82d2fdcd4cea176796f7156"));
           System.out.println("777:"+SighUtil.getMD5(SighUtil.signParam(glRquestParam).replace("\\\\","\\")));
//            System.out.println("888:" + SighUtil.signParam(glRquestParam));
            System.out.println(glRquestParam.toString().replace("\\\\","\\"));


            String s=HttpUtils.doPost("http://demo.api.g2link.cn:8081/","router",glRquestParam.toString().replace("\\\\","\\"));//
            System.out.println(s);


            GoodDetailsRequest goodDetailsRequest = new GoodDetailsRequest();
            goodDetailsRequest.setStartTime("2017-04-26 18:00:00");
            goodDetailsRequest.setEndTime("2017-04-26 18:00:00");
            goodDetailsRequest.setItemCode("");
            goodDetailsRequest.setItemIdWMS("");
            goodDetailsRequest.setProjectCode("");
            goodDetailsRequest.setWarehouseCode("ckys");
            goodDetailsRequest.setPageNo(1);
            goodDetailsRequest.setPageSize(30);


            glRquestParam.setMethod("g2matrix.commodities.goodsDetail");
            glRquestParam.setMd5Sigh(SighUtil.getMD5(SighUtil.signParam(glRquestParam)));
            glRquestParam.setData(ParseJson.parse(goodDetailsRequest));

            String xx=glRquestParam.toString();
            System.out.println(xx);
            String ss=HttpUtils.doPost("http://demo.api.g2link.cn:8081/","router",glRquestParam.toString().replace("\\\\","\\"));//签名获取
            System.out.println(ss);

        }catch (Exception e){ e.printStackTrace();}

    }
}